﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 晨曦超市管理系统.退货
{
    public partial class TuiHuo : Form
    {
        public TuiHuo()
        {
            InitializeComponent();
        }

        private void TuiHuo_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("损坏");
            comboBox1.Items.Add("过期");
            comboBox1.Items.Add("退货");
        }

        DataTable tale;
        public void shua() {
            string A = textBox1.Text.Trim();
            string sqlstr = "select a.Salesman_Name,a.ProductID,b.Name1,a.Quantity,a.AloneAmount,d.Name from SalesDetail a inner join CommodityList b on a.ProductID=b.ProductID inner join Sales c on a.Salesman_Name=c.Salesman_Name inner join informationOfEmployees d on c.UserNameID=d.UserNameID where a.Salesman_Name='" + A + "'";
            Reusing.XianShi(dataGridView1, sqlstr, tale);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            shua();
        }
        //退货函数
        public  void Tuihuo( string shangpin) {
            bool duicuo1 = true;
            try
            {
                string ExceptionID = DateTime.Now.ToString("yyyyMMddHHMMss");
                string UserNameID = Login.UserNameID;
                string ExceptionType = comboBox1.Text;
                string Remark = textBox2.Text.Trim();
                string sqlstr = "insert into GoodsException(ExceptionID,UserNameID,ExceptionType,Remark) values('" + ExceptionID + "'," + UserNameID + ",'" + ExceptionType + "','" + Remark + "')";
                bool duicuo = DBHelper.QEX(sqlstr);
                if (gou)
                {
                    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                    {
                        string ProductID = dataGridView1.Rows[i].Cells[1].Value.ToString();
                        string Quantity = dataGridView1.Rows[i].Cells[4].Value.ToString();
                        string sqlstr1 = "insert into GoodsExceptionDetail(ExceptionID,ProductID,Quantity,Remark) values('" + ExceptionID + "','" + ProductID + "'," + Quantity + ",'" + Remark + "')";
                        duicuo1 = DBHelper.QEX(sqlstr1);
                    } 
                }
                else
                {
                    string ProductID = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                    string Quantity = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                    string sqlstr1 = "insert into GoodsExceptionDetail(ExceptionID,ProductID,Quantity,Remark) values('" + ExceptionID + "','" + ProductID + "'," + Quantity + ",'" + Remark + "')";
                    duicuo1 = DBHelper.QEX(sqlstr1);
                }
                if (duicuo && duicuo1)
                {
                    string sqlstr5 = string.Format("delete from SalesDetail where Salesman_Name='{0}' or ProductID='{1}'", shangpin, shangpin);
                    bool duicuo4 = DBHelper.QEX(sqlstr5);
                    MessageBox.Show("退货成功");
                    textBox1.Text = "";
                    textBox2.Text = "";
                }
                else
                {
                    MessageBox.Show("退货失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        bool gou = true;
        private void button2_Click(object sender, EventArgs e)
        {
            gou = false;
            string ProductID = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            Tuihuo(ProductID);
            dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            gou = true;
            string Salesman_Name = textBox1.Text.Trim();
            Tuihuo(Salesman_Name);
            string sqlstr4 = string.Format("delete from Sales where Salesman_Name='{0}'", Salesman_Name);
            bool duicuo3 = DBHelper.QEX(sqlstr4);
            shua();
        }
    }
}
